import pandas as pd
from cnsenti import Sentiment, Emotion

# 读取 Excel 文件""
file_path = "../../public/data/tables/data_orin.xlsx"  # 替换为你的文件路径
df = pd.read_excel(file_path)

# 初始化 Sentiment 和 Emotion 实例
senti = Sentiment()
emotion = Emotion()

# 创建用于存储结果的列
df['words'] = 0
df['sentences'] = 0
df['pos'] = 0
df['neg'] = 0
df['好'] = 0
df['乐'] = 0
df['哀'] = 0
df['怒'] = 0
df['惧'] = 0
df['恶'] = 0
df['惊'] = 0

# 遍历每一行，进行情感分析
for i, row in df.iterrows():
    text = row['动态文字']
    
    # 进行情感词分析
    sentiment_result = senti.sentiment_count(text)
    df.at[i, 'words'] = sentiment_result['words']
    df.at[i, 'sentences'] = sentiment_result['sentences']
    df.at[i, 'pos'] = sentiment_result['pos']
    df.at[i, 'neg'] = sentiment_result['neg']
    
    # 进行情绪词分析
    emotion_result = emotion.emotion_count(text)
    df.at[i, '好'] = emotion_result['好']
    df.at[i, '乐'] = emotion_result['乐']
    df.at[i, '哀'] = emotion_result['哀']
    df.at[i, '怒'] = emotion_result['怒']
    df.at[i, '惧'] = emotion_result['惧']
    df.at[i, '恶'] = emotion_result['恶']
    df.at[i, '惊'] = emotion_result['惊']

# 将结果保存回 Excel 文件
output_file_path = 'output_file.xlsx'  # 替换为你的输出文件路径
df.to_excel(output_file_path, index=False)

print("情感分析完成，结果已保存到", output_file_path)
